import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email_host = 'smtp.qq.com'
email_port = 25
email_passwd = 'nbzfhttgfwclebha'  # 这个是发送QQ账号的授权码，而不是QQ账号的密码，否则发送会失败
 
sender = 'wikechao@qq.com'  # 发送账号
receivers = ['eplankton@163.com']  # 接收账号
 
msg = MIMEMultipart()
msg['Subject'] = 'From pi g++'
msg['From'] = sender
msg['To'] = ';'.join(receivers)

msg_text = MIMEText(_text='Hello! This email comes from your pi on wikechao@qq.com', _subtype='plain', _charset='utf-8')
msg.attach(msg_text)
 
att = MIMEText(_text=open('camp', 'rb').read(), _subtype='base64', _charset='utf-8')
att['Content-Type'] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment; filename="camp"'
msg.attach(att)
 
try:
    smtpObj = smtplib.SMTP(host=email_host, port=email_port)
    smtpObj.login(sender, email_passwd)
    smtpObj.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email !")
    smtpObj.close()
except smtplib.SMTPException as e:
    print("Error: Unable to send email !")
    print(e)
